package cn.gson.springboot.controllers;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.springboot.common.DateConverter;
import cn.gson.springboot.model.pojos.Bed;
import cn.gson.springboot.model.pojos.Case;
import cn.gson.springboot.model.pojos.Cash;
import cn.gson.springboot.model.pojos.Exchange;
import cn.gson.springboot.model.pojos.Hospital;
import cn.gson.springboot.model.pojos.Leave;
import cn.gson.springboot.model.pojos.MedicalCardPx;
import cn.gson.springboot.model.pojos.OperationRecord;
import cn.gson.springboot.model.pojos.Theater;
import cn.gson.springboot.model.pojos.User;
import cn.gson.springboot.model.service.HospitalService;
import cn.gson.springboot.model.vo.AdviceVO;
import cn.gson.springboot.model.vo.ExamineVO;
import cn.gson.springboot.model.vo.InvoiceVO;


@Controller//住院模块
public class HospitalController {

	@Autowired
	private HospitalService hs;
	
	//科室库存查询
	@RequestMapping("ljw-repertory")
	@ResponseBody
	public Map<String, Object> ljw_repertory(@RequestParam("page")int page,
			@RequestParam("rows")int rows,Integer eid,String dname) {
		//分页
		Page<Object> p = PageHelper.startPage(page,rows);
		JSONArray array = hs.findDeptDrug(eid,dname);
		System.out.println(eid+"科室库存信息--"+array);
		Map<String, Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", array);
		return map;
	}
	
	//体检人身份信息
	@RequestMapping("ljw-examine-exp")
	@ResponseBody
	public JSONArray ljw_examine_exp(Integer eid) {
		JSONArray array = hs.findByEidP(eid);
		System.out.println(eid+"体检人信息--"+array);
		return array;
	}
	
	//体检报告
	@RequestMapping("ljw-examine-exok")
	@ResponseBody
	public JSONArray ljw_examine_exok(Integer eid) {
		JSONArray array = hs.findExOk(eid);
		System.out.println("体检报告--"+array);
		return array;
	}
	
	//体检结果录入
	@RequestMapping("ljw-examine-result")
	@ResponseBody
	public void ljw_examine_result(String json,HttpSession session) {
		User user = (User) session.getAttribute("user");
		JSONArray array = JSONArray.parseArray(json);
		hs.addExR(array,user);
		System.out.println(array);
	}
	
	//体检号查询体检项目
	@RequestMapping("ljw-examine-findByeid")
	@ResponseBody
	public JSONArray ljw_examine_findbyeid(Integer eid) {
		JSONArray array = hs.findByEid(eid);
		System.out.println(eid+"体检："+array);
		return array;
	}
	
	//体检人列表
	@RequestMapping("ljw-examine-find")
	@ResponseBody
	public JSONArray ljw_examine_find(Integer eid) {
		JSONArray array = hs.findExByState(eid);
		System.out.println("体检："+array);
		return array;
	}
	
	//体检登记
	@RequestMapping("ljw-examine-register")
	@ResponseBody
	public boolean ljw_examine_register(ExamineVO xo) {
		System.out.println(xo.getItems().get(0).getExamineItemId()+"--体检人姓名"+xo.getMc().getPatientName());
		try {
			hs.addExamineR(xo,null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} 
		return true;
	}
	
	//体检套餐详情
	@RequestMapping("ljw-combo-details")
	@ResponseBody
	public JSONArray ljw_combo_details(Integer cid) {
		
		return hs.findComboDetails(cid);
	}
	
	//体检套餐
	@RequestMapping("ljw-combo")
	@ResponseBody
	public JSONArray ljw_combo() {
		
		return hs.findCombe();
	}
	
	//单项体检项目
	@RequestMapping("ljw-examine")
	@ResponseBody
	public JSONArray ljw_examine(String ex) {
		
		return hs.findExamine(ex);
	}
	
	//撤销手术
	@RequestMapping("ljw-deleteOps")
	@ResponseBody
	public void ljw_deleteOps(Integer rid){
		hs.deleteOps(rid);
	}
	
	//手术结果
	@RequestMapping("ljw-ops-result")
	@ResponseBody
	public void ljw_ops_result(Integer rid,String result,HttpSession session) {
		try {
			User user = (User) session.getAttribute("user");
			hs.updateResult(rid, result, user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//手术安排记录
	@RequestMapping("ljw-ops-find")
	@ResponseBody
	public JSONArray ljw_ops_find() {
		JSONArray array = hs.findAllOps();
		System.out.println("安排记录---"+array.toString());
		return array;
	}
	
	//新增手术安排
	@RequestMapping("ljw-ops-add")
	@ResponseBody
	public String ljw_ops_add(OperationRecord op,String startTime,String endTime,HttpSession session) {
		Date start = new DateConverter().convert(startTime);
		Time time = Time.valueOf(endTime);
		op.setOperation_start_time(new Timestamp(start.getTime()));
		op.setOperation_end_time(time);
		User user = (User) session.getAttribute("user");
		System.out.println("---护士id--"+op.getUser1().getUserId());
		try {
			boolean opin = hs.addOps(op,user);
			if(opin) {
				return "成功";
			}
			return "失败";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "错误";
		}
	}
	
	//该科室下的手术项目
	@RequestMapping("ljw-ops")
	@ResponseBody
	public JSONArray ljw_ops() {
		return hs.findOps();
	}
	
	//手术室
	@RequestMapping("ljw-theater")
	@ResponseBody
	public JSONArray ljw_theater() {
		JSONArray list = hs.findAllTheater();
		System.out.println("手术室："+list);
		return list;
	}
	
	//费用明细
	@RequestMapping("ljw-clearDetails")
	@ResponseBody
	public JSONArray ljw_clearDetails(Integer hid,Integer type) {
		
		return hs.clearDetails(hid, type);
	}
	
	//出院记录
	@RequestMapping("ljw-leave")
	@ResponseBody
	public Map<String, Object> ljw_leave(@RequestParam("page")int page,
			@RequestParam("rows")int rows,String hid) {
		
		try {
			//分页
			Page<Object> p = PageHelper.startPage(page,rows);
			JSONArray array = hs.findLeave(hid);
			Map<String, Object> map = new HashMap<>();
			map.put("total", p.getTotal());
			map.put("rows", array);
			return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			return null;
		}
	}
	
	//新增出院
	@RequestMapping("ljw-leave-find")
	@ResponseBody
	public JSONArray ljw_leave_find() {
		return hs.findLeave();
	}
	
	
	//新增出院
	@RequestMapping("ljw-leave-add")
	@ResponseBody
	public void ljw_leave_add(Leave leave,HttpSession session) {
		User user = (User) session.getAttribute("user");
		System.out.println(leave.getHospital().getHospitalId()+"---出院=="+leave.getLeaveType());
		hs.addLeave(leave,user);
	}
	
	//手术选择的患者
	@RequestMapping("ljw-leave-patient")
	@ResponseBody
	public JSONArray ljw_leave_patient(String hid,HttpSession session) {
		User user = (User) session.getAttribute("user");
		System.out.println("科室id=="+user.getDepartmentsPx().getDepartmentsId());
		JSONArray array = hs.findLeavePatient(hid,user.getDepartmentsPx().getDepartmentsId());
		System.out.println("被手术者信息=="+array);
		return array;
	}
	
	//费用明细
	@RequestMapping("ljw-cost")
	@ResponseBody
	public JSONArray ljw_cost(Integer hid,Integer opin) {
		JSONArray array = hs.findCost(hid,opin);
		System.out.println(hid+"--费用=="+array);
		return array;
	}
	
	//执行医嘱
	@RequestMapping("ljw-ex-advice")
	@ResponseBody
	public String ljw_ex_advice(Integer aid,HttpSession session) {
		try {
			User user = (User) session.getAttribute("user");
			boolean opin = hs.exAdvice(aid , user);
			if(opin) {
				return "成功";
			}else {
				return "库存不足";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "失败";
		}
	}
	
	//请领详情
	@RequestMapping("ljw-invoice-details")
	@ResponseBody
	public Map<String, Object> ljw_invoice_details(@RequestParam("page")int page,
			@RequestParam("rows")int rows,Integer inid) {
		//分页
		Page<Object> p = PageHelper.startPage(page,rows);
		JSONArray array = hs.findInvoiceDetails(inid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", array);
		return map;
	}
	
	//请领记录
	@RequestMapping("ljw-invoice-record")
	@ResponseBody
	public Map<String, Object> ljw_invoice_record(@RequestParam("page")int page,
			@RequestParam("rows")int rows,String dname) {
		//分页
		Page<Object> p = PageHelper.startPage(page,rows);
		JSONArray array = hs.findInvoice(dname);
		Map<String, Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", array);
		return map;
	}
	
	//药品请领
	@RequestMapping("ljw-invoice")
	@ResponseBody
	public String ljw_invoice(InvoiceVO iv) {
		try {
			System.out.println("用药申请："+iv.getDetails().get(0).getInvoiceDetailsNumber());
			hs.addInvoice(iv);
			return "1";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "0";
		}
	}
	
	//住院号查询医嘱执行记录
	@RequestMapping("ljw-advice-record")
	@ResponseBody
	public JSONArray record(Integer hid) {
		JSONArray array = hs.findAdviceRecord(hid);
		System.out.println("医嘱执行记录："+array);
		return array;
	}
	
	//住院号查询医嘱详情
	@RequestMapping("ljw-advice-details")
	@ResponseBody
	public JSONArray ljw_advice_details(Integer hid) {
		JSONArray array = hs.findAdviceDetails(hid);
		System.out.println("医嘱详情："+array);
		return array;
	}
	
	//患者个人信息
	@RequestMapping("ljw-patient-data")
	@ResponseBody
	public JSONArray ljw_patient_data(Integer hid) {
		JSONArray array = hs.findPatientDate(hid);
		System.out.println("身份信息："+array);
		return array;
	}
	
	//新增病历
	@RequestMapping("ljw-case")
	@ResponseBody
	public boolean ljw_case(Case c) {
		System.out.println(c.getCaseComplaint()+"---病历");
		try {
			hs.addCase(c);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	//停嘱
	@RequestMapping("ljw-updateAdvice")
	@ResponseBody
	public void  ljw_updateAdvice(Integer aid) {
		hs.updateAdvice(aid);
	}
	
	//医嘱详情
	@RequestMapping("ljw-adDetils")
	@ResponseBody
	public JSONArray  ljw_adDetils(Integer aid) {
		JSONArray array = hs.findAdDetail(aid);
		System.out.println("医嘱=="+array);
		return array;
	}
	
	//医嘱管理
	@RequestMapping("ljw-advice-find")
	@ResponseBody
	public Map<String, Object>  ljw_advice_find(int page,int rows,HttpSession session,String str) {
		User user = (User) session.getAttribute("user");
		System.out.println("--科室id---"+user.getDepartmentsPx().getDepartmentsId());
		//分页
		Page<Object> p = PageHelper.startPage(page,rows);
		JSONArray array = hs.findAdvice(user.getDepartmentsPx().getDepartmentsId(),str);
		System.out.println("医嘱管理=="+array);
		Map<String, Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", array);
		return map;
	}
	
	//新增医嘱
	@RequestMapping("ljw-advice-add")
	@ResponseBody
	public void ljw_advice_add(AdviceVO av,HttpSession session) {
		System.out.println(av.getAdvice().getHospital().getHospitalId()+"---天数");
		User user = (User) session.getAttribute("user");
		hs.addAdvice(av,user);
	}
	
	//医嘱录入查询药品
	@RequestMapping("ljw-drug")
	@ResponseBody
	public Map<String, Object> ljw_drug(@RequestParam("page")int page,
			@RequestParam("rows")int rows,Integer tid,String name) {
		System.out.println(tid+"---"+name.trim());
		//分页
		Page<Object> p = PageHelper.startPage(page,rows);
		//查询药品
		JSONArray array = hs.findDrug(tid, "%"+name.trim()+"%");
		Map<String, Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", array);
		System.out.println("药品："+array);
		return map;
	}
	
	//医生站界面
	@RequestMapping("hospital-doctor")
	public void hospital_doctor(Model model,HttpSession session) {
		User user = (User) session.getAttribute("user");
		//查询患者
		JSONArray array = hs.findPatient(user.getDepartmentsPx().getDepartmentsId());
		System.out.println("医师患者："+array);
		model.addAttribute("array",array);
	} 
	
	//护士站界面
	@RequestMapping("hospital-nurse")
	public void hospital_nurse(Model model) {
		//查询患者
		JSONArray array = hs.findPatient(5);
		System.out.println("护士患者："+array);
		model.addAttribute("array",array);
	}
	
	//床位新增
	@RequestMapping("ljw-addBed")
	@ResponseBody
	public String ljw_addBed(String bed,HttpSession session) {
		System.out.println(bed);
		try {
			User user = (User) session.getAttribute("user");
			hs.addBed(bed,user);
			return "成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "异常";
		}
		
	}
	
	//床位调整记录
	@RequestMapping("ljw-exchange-record")
	@ResponseBody
	public JSONArray ljw_exchange_record(String hid) {
		System.out.println(hid);
		JSONArray array = hs.findExchange(hid);
		
		return array;
	}
	
	//床位调整
	@RequestMapping("ljw-bed-exchange")
	@ResponseBody
	public Integer ljw_bed_exchange(Exchange ex,Integer hid,Integer oldBid,Integer nowBid) {
		Hospital hospital = hs.findHospitalByid(hid);
		Bed old = hs.findBedByid(oldBid);
		Bed now = hs.findBedByid(nowBid);
		hs.exchange_bed(ex, old, now, hospital);
		System.out.println("hid---"+hid);
		return hospital.getDepartmentsPx().getDepartmentsId();
	}
	
	//床位查询
	@RequestMapping("ljw-bed-all")
	@ResponseBody
	public Map<String, Object> ljw_bed_all(
			@RequestParam("page")int page,@RequestParam("rows")int rows,
			String hid,Integer did) {
		//分页
		Page<Object> p = PageHelper.startPage(page,rows);
		//床位查询
		JSONArray array = hs.findBed(hid,did);
		System.out.println(array);
		Map<String, Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", array);
		return map;
	}
	
	//预交押金和结算
	@RequestMapping("ljw-cash-pay")
	@ResponseBody
	public boolean ljw_cash_pay(Cash cash,Integer i,HttpSession session) {
		User user = (User) session.getAttribute("user");
		System.out.println(cash.getHospital().getHospitalId()+"-预交押金--"+cash.getCashMoney());
		try {
			hs.addCash(cash,i,user.getUserName());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	//催缴单
	@RequestMapping("ljw-cash-payment")
	@ResponseBody
	public String ljw_cash_payment(Integer hid) {
		JSONArray array = hs.findPayment(hid);
		System.out.println("押金记录："+array);
		return array.toJSONString();
	}
	
	//押金记录
	@RequestMapping("ljw-cash-record")
	@ResponseBody
	public Map<String, Object> ljw_cash_record(@RequestParam("page")int page,
			@RequestParam("rows")int rows,String hid) {
		Page<Object> p = PageHelper.startPage(page,rows);
		JSONArray array = hs.findCashRecord(hid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", array);
		System.out.println("押金记录："+array);
		System.out.println("---押金记录-----");
		return map;
	}

	//押金统计
	@RequestMapping("ljw-cash-total")
	@ResponseBody
	public Map<String, Object> ljw_cash(@RequestParam("page")int page,
			@RequestParam("rows")int rows,String hid) {
		Page<Object> p = PageHelper.startPage(page,rows);
		JSONArray array = hs.findCashTotal(hid);
		System.out.println("统计++++++++="+array);
		Map<String, Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", array);
		return map;
	}
	
	//住院记录
	@RequestMapping("ljw-record-hos")
	@ResponseBody
	public Map<String, Object> ljw_record_hos(@RequestParam("page")int page,
				@RequestParam("rows")int rows,String hid) {
		Page<Object> p = PageHelper.startPage(page,rows);
		JSONArray array = hs.findHospital(hid);//查询所有住院记录
		Map<String, Object> map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", array);
		return map;
	}
	
	//住院登记
	@PostMapping("ljw-register")
	@ResponseBody
	public String ljw_register(MedicalCardPx card,Cash cash,Integer did,
			Integer bed_id,Hospital hos,HttpSession session) {
		//登记
		try {
			User user =  (User) session.getAttribute("user");
			boolean opin = hs.register(card, cash, did,bed_id ,hos ,user);
			if(opin) {
				return "成功";
			}
			return "正在住院中，请勿重复登记";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "错误";
		}
	}
	
	//住院通知查询医疗卡
	@RequestMapping("ljw-medical")
	@ResponseBody
	public MedicalCardPx ljw_medical(String mid) {
		System.out.println("住院通知--"+mid);
		MedicalCardPx card = hs.findMedByid(mid);
		return card;
	}
	
	
	//所有科室
	@RequestMapping("ljw-departments")
	@ResponseBody
	public JSONArray ljw_departments() {
		System.out.println(hs.findDepartments());

		return hs.findDepartments();
	}
	//所有科室
		@RequestMapping("cxsyks")
		@ResponseBody
		public JSONArray cxsyks() {
			System.out.println(hs.cxsyks());

			return hs.cxsyks();
		}
	
	//科室下的角色
	@RequestMapping("ljw-role")
	@ResponseBody
	public JSONArray ljw_role(Integer did,Integer rid) {
		JSONArray array = hs.findRoleAndDept(did,rid);
		return array;
	}
	
	//科室下的空床位
	@RequestMapping("ljw-bed")
	@ResponseBody
	public JSONArray ljw_bed(Integer did) {
		JSONArray array = hs.findBedByDept(did);
		System.out.println("空床位:"+array);
		return array;
	}
	
}  
